//
//  XMDynamicModel.m
//  XunMi
//
//  Created by MacBook on 2024/3/6.
//  Copyright © 2024 TieZhu. All rights reserved.
//

#import "XMDynamicModel.h"

static CGFloat const kMaxContentH = 55;

@implementation XMDynamicModel

+ (NSDictionary *)mj_replacedKeyFromPropertyName {
    return @{
        @"xmId": @"id",
        @"xmModule": @"module",
    };
}

+ (NSDictionary *)mj_objectClassInArray {
    return @{
        @"resource":@"XMFileModel",
        @"avatar":@"XMFileModel"
    };
}

// 当字典转模型完毕时调用
- (void)mj_didConvertToObjectWithKeyValues:(NSDictionary *)keyValues {
    self.showMore = ([self.content heightForFont:kUIFont(14) width:kScreenWidth - 40] + 1) > kMaxContentH ? YES : NO;
}

- (CGFloat)cellH {
    CGFloat cellH = 0.;
    cellH += 20;
    cellH += 40;
    cellH += self.content.length > 0 ? 12 : 0;
    cellH += [self contentH];
    cellH += 6;
    cellH += self.showMore ? 20 : 0;
    cellH += 6;
    cellH += [self imageViewH];
    cellH += 44;
    cellH += 10;
    return cellH;
}

- (CGFloat)otherCellH {
    CGFloat cellH = 0.;
    cellH += 20;
    cellH += 15;
    cellH += self.content.length > 0 ? 12 : 0;
    cellH += [self contentH];
    cellH += 6;
    cellH += self.showMore ? 20 : 0;
    cellH += 6;
    cellH += [self imageViewH];
    cellH += self.resource.count > 0 ? 20 : 8;
    cellH += 10;
    return cellH;
}

- (CGFloat)imageViewH {
    CGFloat imageViewH = 0.;
    switch (self.resource.count) {
        case 0:
            return 0;
            break;
        case 1:
            return 188;
            break;
        case 2:
            return (kScreenWidth - 50) / 2;
            break;
        case 3:
        case 4:
        case 5:
        case 6:
        case 7:
        case 8:
        case 9:
            return (kScreenWidth - 60) / 3;
            break;
        default:
            return 0;
            break;
    }
    return imageViewH;
}

- (CGFloat)contentH {
    if (self.content.length == 0) {
        return 0;
    }
    CGFloat contentH = 0.f;
    contentH = [self.content heightForFont:kUIFont(14) width:kScreenWidth - 40] + 1;
    if (self.isOpening) {
        return contentH;
    } else {
        return MIN(contentH, kMaxContentH);
    }
}

- (void)setIsOpening:(BOOL)isOpening {
    if (!_showMore) {
        _isOpening = NO;
    } else {
        _isOpening = isOpening;
    }
}

@end
